﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities; //Must be present to use the DLL entities
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace RAD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //Deserializes RAD_Config.xml to get the Current Model and Version
            Radar info = new Radar();
            TextReader reader = null;

            try
            {
                reader = new StreamReader("../../XML/RAD_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Radar));
                info = (Radar)serializer.Deserialize(reader);

                reader.Close();
                //The model and version are transfered into the model and version text boxes on the form designer.
                txtModel.Text = Convert.ToString(info.Model);
                txtVersion.Text = Convert.ToString(info.Version);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            //Reads the status of the Radar health: Online/Offline
            Radar status = new Radar();

            status.RadarStatus = "Online";

            XmlSerializer serializer1 = new XmlSerializer(typeof(Radar));
            TextWriter textWriter = new StreamWriter("../../XML/RAD_Status.xml");
            serializer1.Serialize(textWriter, status);
            textWriter.Close();

            //try
            //{
            //    File.Delete("../../XML/RAD_Event.xml");
            //}
            //catch (FileNotFoundException file)
            //{
            //}

            //The timer feature is enabled and runs immediately on form load
            //timer1.Enabled = true;
        }

        private void btnAcquire_Click(object sender, EventArgs e)
        {
            //Call the random Number method from the Utility class- min and max values are established 
            //int returnValue = Utility.RandomNumber(1, 500);
            //txtRange.Text = Convert.ToString(returnValue);
            txtRange.Text = Utility.RandomNumber(1, 500).ToString();
            txtAltitude.Text = Utility.RandomNumber(1, 13501).ToString();
            txtSpeed.Text = Utility.RandomNumber(1, 501).ToString();
            txtDirection.Text = Utility.RandomNumber(1, 361).ToString();

            //Used an array for storing and displaying the heading in string type
            string[] headingArray = { "N", "NE", "NW", "S", "SE", "SW", "E", "W" };
            int headingIndex = Utility.RandomNumber(0, 7);
            txtHeading.Text = headingArray[headingIndex];

            //Radial buttons are not enabled so the user can not manually select them
            radBird.Enabled = false;
            radMissile.Enabled = false;
            radShip.Enabled = false;
            radPlane.Enabled = false;


            //Setting conditions for Missile, Bird, Ship, and Plane. Once a condition is met, the respective radial button is selected
            if (Convert.ToInt16(txtSpeed.Text) >= 400)
            {
                radMissile.Select();
                radMissile.Enabled = true;
            }

            else if (Convert.ToInt16(txtAltitude.Text) <= 100 && (Convert.ToInt16(txtSpeed.Text) <= 5)) 
            {
                radBird.Select();
                radBird.Enabled = true;
            }

            else if (Convert.ToInt16(txtAltitude.Text) <= 45 && (Convert.ToInt16(txtSpeed.Text) > 5))
            {
                radShip.Select();
                radShip.Enabled = true;
            }

            else if (Convert.ToInt16(txtAltitude.Text) >=101 && (Convert.ToInt16(txtSpeed.Text) < 400))
            {
                radPlane.Select();
                radPlane.Enabled = true;
            }
            //Lock-On button was disabled on form load to prevent the user from trying to click it.
            //Once the Acquire button is clicked, the Lock-On button is enabled.
            btnLock.Enabled = true;

        }

        

        
        //Using multiple else/if statements to determine which button selection will identify the target name
        private void btnLock_Click(object sender, EventArgs e)
        {
            Targets target = new Targets();

            if (radBird.Checked)
            {
                target.TargetName = radBird.Text;
            }
            else if (radPlane.Checked)
            {
                target.TargetName = radPlane.Text;
            }
            else if (radShip.Checked)
            {
                target.TargetName = radShip.Text;
            }
            else if (radMissile.Checked)
            {
                target.TargetName = radMissile.Text;
            }

            //Once the target information has been generated, the info is displayed into the respective text boxes on the form designer
            target.TargetAltitude = Convert.ToInt16(txtAltitude.Text);
            target.TargetDirection = Convert.ToInt16(txtDirection.Text);
            target.TargetDistance = Convert.ToInt16(txtRange.Text);
            target.TargetSpeed = Convert.ToInt16(txtSpeed.Text);
            target.TargetNav = txtHeading.Text;

            XmlSerializer serializer = new XmlSerializer(typeof(Targets));
            TextWriter textWriter = new StreamWriter("../../XML/RAD_Event.xml");
            serializer.Serialize(textWriter, target);
            textWriter.Close();

            //This series of if/else statements will clear out the radials once the Lock-On button is clicked.
            if (radPlane.Checked)
            {
                radPlane.Checked = false;
                radPlane.Enabled = false;
            }
            else if (radBird.Checked)
            {
                radBird.Checked = false;
                radBird.Enabled = false;
            }
            else if (radShip.Checked)
            {
                radShip.Checked = false;
                radShip.Enabled = false;
            }
            else if (radMissile.Checked)
            {
                radMissile.Checked = false;
                radMissile.Enabled = false;
            }
            //Once the Lock-On button is clicked, the text boxes on the form designer are cleared out.
            txtAltitude.Text = "";
            txtDirection.Text = "";
            txtRange.Text = "";
            txtSpeed.Text = "";
            txtHeading.Text = "";

            btnLock.Enabled = false;
        }

        //private void timer1_Tick(object sender, EventArgs e)
        //{

        //}
        
        
        

        
       

       

       

        

       
    }
}
